package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IImageCleanupService;

/**
 * 图片清理管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-XX
 */
@RestController
@RequestMapping("/system/imageCleanup")
public class ImageCleanupController extends BaseController
{
    @Autowired
    private IImageCleanupService imageCleanupService;

    /**
     * 获取清理任务状态
     */
    @PreAuthorize("@ss.hasPermi('system:imageCleanup:query')")
    @GetMapping("/status")
    public AjaxResult getStatus()
    {
        String status = imageCleanupService.getCleanupStatus();
        return success(status);
    }

    /**
     * 获取无引用图片列表
     */
    @PreAuthorize("@ss.hasPermi('system:imageCleanup:list')")
    @GetMapping("/unreferencedList")
    public AjaxResult getUnreferencedList()
    {
        List<String> unreferencedImages = imageCleanupService.getUnreferencedImageList();
        return success(unreferencedImages);
    }

    /**
     * 手动执行清理任务
     */
    @PreAuthorize("@ss.hasPermi('system:imageCleanup:cleanup')")
    @Log(title = "图片清理", businessType = BusinessType.CLEAN)
    @PostMapping("/cleanup")
    public AjaxResult cleanup()
    {
        try {
            int cleanedCount = imageCleanupService.cleanupUnreferencedImages();
            return success("清理完成，共清理 " + cleanedCount + " 个无引用图片");
        } catch (Exception e) {
            return error("清理失败：" + e.getMessage());
        }
    }

    /**
     * 删除业务关联的图片
     */
    @PreAuthorize("@ss.hasPermi('system:imageCleanup:remove')")
    @Log(title = "图片清理", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBusinessImages")
    public AjaxResult deleteBusinessImages(@RequestParam String businessType, 
                                         @RequestParam String businessId,
                                         @RequestParam(required = false) String businessField)
    {
        try {
            int deletedCount = imageCleanupService.deleteBusinessImages(businessType, businessId, businessField);
            return success("删除完成，共删除 " + deletedCount + " 个业务图片");
        } catch (Exception e) {
            return error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 删除用户关联的所有图片
     */
    @PreAuthorize("@ss.hasPermi('system:imageCleanup:remove')")
    @Log(title = "图片清理", businessType = BusinessType.DELETE)
    @PostMapping("/deleteUserImages")
    public AjaxResult deleteUserImages(@RequestParam Long userId)
    {
        try {
            int deletedCount = imageCleanupService.deleteUserImages(userId);
            return success("删除完成，共删除 " + deletedCount + " 个用户图片");
        } catch (Exception e) {
            return error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 手动删除指定图片
     */
    @PreAuthorize("@ss.hasPermi('system:imageCleanup:remove')")
    @Log(title = "图片清理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public AjaxResult removeImage(@RequestParam String fileName)
    {
        try {
            boolean success = imageCleanupService.cleanupImage(fileName);
            if (success) {
                return success("图片删除成功");
            } else {
                return error("图片删除失败");
            }
        } catch (Exception e) {
            return error("删除失败：" + e.getMessage());
        }
    }

}
